package com.cinemahorairerapido.adapter;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cinemahorairerapido.R;
import com.cinemahorairerapido.bean.Movie;
import com.cinemahorairerapido.bean.Movie.ShowTimeType;

public class CinemaProgramAdapter extends BaseAdapter {

	private List<Movie> mCinemaProgramList = new ArrayList<Movie>();

	private final Date showTimeDate;

	Context context;

	LayoutInflater inflater;

	private class ViewHolder {
		TextView tvName;
		TextView tvTimes;
	}

	public CinemaProgramAdapter(List<Movie> cinemaProgramList, Date date, Context context) {
		super();
		this.mCinemaProgramList = cinemaProgramList;
		this.context = context;
		this.showTimeDate = date;

		inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return mCinemaProgramList.size();
	}

	@Override
	public Object getItem(int position) {
		return mCinemaProgramList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return mCinemaProgramList.get(position).getId();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder;

		if (convertView == null) {

			holder = new ViewHolder();

			convertView = inflater.inflate(R.layout.cinemaprogramrow, null);

			holder.tvName = (TextView) convertView.findViewById(R.id.movieName);
			holder.tvTimes = (TextView) convertView.findViewById(R.id.movieTimes);

			convertView.setTag(holder);

		} else {

			holder = (ViewHolder) convertView.getTag();

		}

		Movie movie = mCinemaProgramList.get(position);

		holder.tvName.setText(movie.getMovieName() /*
													 * + (movie.getIs3D() ?
													 * " 3D" : "") +
													 * (movie.getIsVf() ? " VF"
													 * : " VO")
													 */);
		holder.tvTimes.setText(formatTimeList(showTimeDate, movie.getTimeList()));

		return convertView;

	}

	protected String formatTimeList(Date showDate, HashMap<Date, HashMap<ShowTimeType, List<String>>> timeList) {

		String timeFormated = "";

		for (ShowTimeType type : timeList.get(showDate).keySet()) {

			if (type.equals(ShowTimeType.VO)) {
				timeFormated += "VO : ";
			} else if (type.equals(ShowTimeType.VF3D)) {
				timeFormated += "VF 3D : ";
			} else if (type.equals(ShowTimeType.VO3D)) {
				timeFormated += "VO 3D : ";
			} else if (type.equals(ShowTimeType.IMAX)) {
				timeFormated += "VF IMAX : ";
			} else if (type.equals(ShowTimeType.VOIMAX)) {
				timeFormated += "VO IMAX : ";
			} else {
				timeFormated += "VF : ";
			}

			for (String t : timeList.get(showDate).get(type)) {
				timeFormated += t + " ";
			}

			timeFormated += "\n";
		}

		// context.getResources().getString(R.string.showtimelist) + " ";

		// SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");

		// timeFormated = format.format(showDate) + ": ";

		// for (String t : timeList.get(showDate)) {
		// timeFormated += t + " ";
		// }

		// timeFormated += "\n";

		return timeFormated;
	}
}